package com.hmall.item.control;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.impl.ItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author liCuiPing
 * @since 2022/12/8 14:40
 */
@RestController
@RequestMapping("item")
public class IItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long itemId,
                            @PathVariable("num") Integer num){
        itemService.deduct(itemId,num);
    }

    /**
     * 管理端分页
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> findPage(@RequestParam("page") Integer pageNumber,
                                  @RequestParam("size") Integer pageSize) {
        IPage<Item> pageList = itemService.getItemList(pageNumber, pageSize);
        List<Item> list = pageList.getRecords();
        long total = pageList.getTotal();
        return new PageDTO<>(total,list);
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping
    public void saveItem(@RequestBody Item item) {
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(2);
        itemService.saveItem(item);
        System.out.println("新增成功");
    }

    /**
     * 上下架,状态更新
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id ,
                             @PathVariable("status") Integer status) {
        itemService.updateById(id ,status);



        System.out.println("mysql状态更新成功");
    }

    /**
     * 修改
     * @param item
     */
    @PutMapping()
    public void updateAll(@RequestBody Item item) {
        itemService.updateAllById(item);
    }

    /**
     * 根据id删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        itemService.deleteById(id);
        System.out.println("删除成功");
    }

    /**
     * 根据id查一个商品
     * @param id
     */
    @GetMapping("/{id}")
    public Item findById(@PathVariable Long id) {
        return itemService.findById(id);
    }
}